<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Task;
use App\Models\TaskServiceType;
use App\Models\Tenant;
use Carbon\Carbon;

class TaskSeeder extends Seeder
{
    /**
     * 运行任务数据填充
     *
     * @return void
     */
    public function run()
    {
        $tenant = Tenant::first();
        if (! $tenant) {
            $this->command?->warn('TaskSeeder: 未找到租户数据，建议先运行 TenantSeeder。');
            return;
        }
        if ($tenant->service_fee_rate === null) {
            $tenant->service_fee_rate = 0.05;
            $tenant->save();
        }

        $defaultServiceType = TaskServiceType::query()->value('name') ?? '信息技术服务';

        $tasks = [
            [
                'tenant_id' => $tenant->id,
                'name' => 'PPT制作',
                'service_type' => TaskServiceType::where('name', '设计服务')->value('name') ?? $defaultServiceType,
                'start_time' => '2025-06-17',
                'end_time' => '2026-06-16',
                'settlement_cycle' => 'per_task',
                'min_fee' => 10000.00,
                'max_fee' => 80000.00,
                'billing_unit' => '件',
                'gender_requirement' => 'unlimited',
                'education_requirement' => 'unlimited',
                'min_age' => 18,
                'max_age' => 65,
                'work_location' => '全国',
                'work_time' => '灵活工作时间',
                'work_content' => '负责制作各类PPT演示文稿，要求熟练掌握PowerPoint软件操作，具备良好的设计能力和沟通能力。工作时间灵活，按项目计费。',
                'work_compensation' => '按项目计费',
                'work_benefits' => '时间灵活，远程工作',
                'status' => 'active',
                'qr_code_url' => null,
                'created_at' => Carbon::now()->subDays(5),
                'updated_at' => Carbon::now()->subDays(5),
            ],
            [
                'tenant_id' => $tenant->id,
                'name' => '网站开发项目',
                'service_type' => TaskServiceType::where('name', '信息技术服务')->value('name') ?? $defaultServiceType,
                'start_time' => Carbon::now()->addDays(10),
                'end_time' => Carbon::now()->addDays(70),
                'settlement_cycle' => 'monthly',
                'min_fee' => 10000.00,
                'max_fee' => 15000.00,
                'billing_unit' => '月',
                'gender_requirement' => 'unlimited', // 修改：不能为 null
                'education_requirement' => 'bachelor', // 修改：使用英文枚举值
                'min_age' => 25,
                'max_age' => 40,
                'work_location' => '上海市浦东新区张江',
                'work_time' => '弹性工作时间',
                'work_content' => '开发企业官网，包括前端页面制作、后端接口开发、数据库设计等',
                'work_compensation' => '按月结算',
                'work_benefits' => '五险一金，年终奖',
                'status' => 'active', // 修改：使用正确的枚举值
                'qr_code_url' => null,
                'created_at' => Carbon::now()->subDays(3),
                'updated_at' => Carbon::now()->subDays(3),
            ],
            [
                'tenant_id' => $tenant->id,
                'name' => '数据分析师',
                'service_type' => TaskServiceType::where('name', '现代服务')->value('name') ?? $defaultServiceType,
                'start_time' => Carbon::now()->addDays(5),
                'end_time' => Carbon::now()->addDays(35),
                'settlement_cycle' => 'daily', // 修改：使用 daily 而不是 weekly
                'min_fee' => 500.00,
                'max_fee' => 800.00,
                'billing_unit' => '日',
                'gender_requirement' => 'unlimited', // 修改：不能为 null
                'education_requirement' => 'master', // 修改：使用英文枚举值
                'min_age' => 24,
                'max_age' => 35,
                'work_location' => '深圳市南山区科技园',
                'work_time' => '周一至周五 9:00-18:00',
                'work_content' => '负责用户行为数据分析，制作数据报表，提供业务洞察',
                'work_compensation' => '按日结算',
                'work_benefits' => '弹性工作，远程办公',
                'status' => 'active', // 修改：使用正确的枚举值
                'qr_code_url' => null,
                'created_at' => Carbon::now()->subDays(2),
                'updated_at' => Carbon::now()->subDays(2),
            ],
            [
                'tenant_id' => $tenant->id,
                'name' => '内容运营专员',
                'service_type' => TaskServiceType::where('name', '其他咨询服务')->value('name') ?? $defaultServiceType,
                'start_time' => Carbon::now()->addDays(3),
                'end_time' => Carbon::now()->addDays(33),
                'settlement_cycle' => 'daily', // 修改：使用有效的枚举值
                'min_fee' => 50.00,
                'max_fee' => 80.00,
                'billing_unit' => '小时',
                'gender_requirement' => 'female', // 修改：使用英文枚举值
                'education_requirement' => 'college', // 修改：使用英文枚举值
                'min_age' => 20,
                'max_age' => 30,
                'work_location' => '广州市天河区珠江新城',
                'work_time' => '灵活安排',
                'work_content' => '负责社交媒体内容创作、用户互动、活动策划等工作',
                'work_compensation' => '按小时结算',
                'work_benefits' => '灵活工作时间，提供培训机会',
                'status' => 'inactive', // 修改：使用正确的枚举值
                'qr_code_url' => null,
                'created_at' => Carbon::now()->subDays(1),
                'updated_at' => Carbon::now()->subDays(1),
            ],
            [
                'tenant_id' => $tenant->id,
                'name' => '市场推广助理',
                'service_type' => TaskServiceType::where('name', '现代服务')->value('name') ?? $defaultServiceType,
                'start_time' => Carbon::now()->addDays(14),
                'end_time' => Carbon::now()->addDays(44),
                'settlement_cycle' => 'per_task', // 修改：使用正确的枚举值
                'min_fee' => 2000.00,
                'max_fee' => 3500.00,
                'billing_unit' => '月',
                'gender_requirement' => 'unlimited', // 修改：不能为 null
                'education_requirement' => 'bachelor', // 修改：使用英文枚举值
                'min_age' => 22,
                'max_age' => 28,
                'work_location' => '杭州市西湖区',
                'work_time' => '工作日 10:00-19:00',
                'work_content' => '协助市场推广活动策划执行，客户关系维护，市场调研等',
                'work_compensation' => '项目完成后支付',
                'work_benefits' => '团队建设活动，节日福利',
                'status' => 'inactive', // 修改：使用正确的枚举值
                'qr_code_url' => null,
                'created_at' => Carbon::now()->subHours(12),
                'updated_at' => Carbon::now()->subHours(12),
            ],
        ];

        foreach ($tasks as $taskData) {
            Task::create($taskData);
        }

        $this->command->info('任务数据填充完成！创建了 ' . count($tasks) . ' 个测试任务。');
    }
}
